package com.cta.entity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.joda.money.BigMoney;
import org.joda.time.DateTime;
import org.joda.time.LocalTime;

import com.cta.constant.ProfileUserMapping;

public class CallReport {
	private String pbxNo;
	private String trunkId;
	private String employeeNum;
	private String callingNum;
	private String dialedNum;
	private String destination;
	private DateTime startDate;
	private LocalTime startTime;
	private DateTime endDate;
	private long duration;
	private String durationStr;
	private BigMoney price;
	private String priceStr;
	private String discount;
	private Long userId;
	private String fullUserName;
	private String telCo;

	private Map<String, Object> data;

	public Object[] getInsertSql(String table) {
		StringBuilder mainStr = new StringBuilder();
		mainStr.append("INSERT INTO ").append(table).append(" (");
		StringBuilder keyStr = new StringBuilder();
		StringBuilder valStr = new StringBuilder();
		List<Object> vals = new ArrayList<Object>();
		for (String key : data.keySet()) {
			keyStr.append(key + ", ");
			valStr.append("?").append(", ");
			if (ProfileUserMapping.def_trunk_no.name().equals(key)) {
				vals.add(trunkId);
			} else if (ProfileUserMapping.def_start_date.name().equals(key)) {
				vals.add(startDate.toDate());
			} else if (ProfileUserMapping.def_start_time.name().equals(key)) {
				vals.add(startTime.toDateTimeToday().toDate());
			} else if (ProfileUserMapping.def_duration.name().equals(key)) {
				vals.add(duration);
			} else if (ProfileUserMapping.def_dialed_number.name().equals(key)) {
				vals.add(dialedNum);
			} else if (ProfileUserMapping.def_calling_number.name().equals(key)) {
				vals.add(callingNum);
			} else if (ProfileUserMapping.def_author_code.name().equals(key)) {
				vals.add("".equals(employeeNum)?null: employeeNum);
			} else {
				vals.add(data.get(key));
			}
		}
		// add some static data
		keyStr.append("def_pbx_no, ");
		valStr.append("?").append(", ");
		vals.add(pbxNo);
		
		keyStr.append("def_end_date, ");
		valStr.append("?").append(", ");
		vals.add(endDate.toDate());
		
		keyStr.append("def_telco, ");
		valStr.append("?").append(", ");
		vals.add(telCo);

		keyStr.append("def_user_id, ");
		valStr.append("?").append(", ");
		vals.add(userId);
		
		keyStr.append("def_destination, ");
		valStr.append("?").append(", ");
		vals.add(destination);

		keyStr.append("def_price, ");
		valStr.append("?").append(", ");
		vals.add(price.getAmount());

		keyStr.append("def_discount, ");
		valStr.append("?").append(", ");
		vals.add(discount);

		keyStr.append("def_reg_date, ");
		valStr.append("now(), ");
		// generate UUID
		keyStr.append("uid");
		valStr.append("?");
		vals.add(UUID.randomUUID());

		mainStr.append(keyStr.toString()).append(" ) ").append("VALUES (").append(valStr.toString()).append(" ) ");
		return new Object[]{mainStr.toString(), vals.toArray()};
	}

	public String getTrunkId() {
		return trunkId;
	}

	public void setTrunkId(String trunkId) {
		this.trunkId = trunkId;
	}

	public String getEmployeeNum() {
		return employeeNum;
	}

	public void setEmployeeNum(String employeeNum) {
		this.employeeNum = employeeNum;
	}

	public String getCallingNum() {
		return callingNum;
	}

	public void setCallingNum(String callingNum) {
		this.callingNum = callingNum;
	}

	public String getDialedNum() {
		return dialedNum;
	}

	public void setDialedNum(String dialedNum) {
		this.dialedNum = dialedNum;
	}

	public String getDestination() {
		return destination;
	}

	public void setDestination(String destination) {
		this.destination = destination;
	}

	public DateTime getStartDate() {
		return startDate;
	}

	public void setStartDate(DateTime startDate) {
		this.startDate = startDate;
	}

	public DateTime getEndDate() {
		return endDate;
	}

	public void setEndDate(DateTime endDate) {
		this.endDate = endDate;
	}

	public long getDuration() {
		return duration;
	}

	public void setDuration(long duration) {
		this.duration = duration;
	}

	public BigMoney getPrice() {
		return price;
	}

	public void setPrice(BigMoney price) {
		this.price = price;
	}

	public String getDiscount() {
		return discount;
	}

	public void setDiscount(String discount) {
		this.discount = discount;
	}

	public String getPriceStr() {
		return priceStr;
	}

	public void setPriceStr(String priceStr) {
		this.priceStr = priceStr;
	}

	public Map<String, Object> getData() {
		return data;
	}

	public void setData(Map<String, Object> data) {
		this.data = data;
	}

	public String getDurationStr() {
		return durationStr;
	}

	public void setDurationStr(String durationStr) {
		this.durationStr = durationStr;
	}

	public LocalTime getStartTime() {
		return startTime;
	}

	public void setStartTime(LocalTime startTime) {
		this.startTime = startTime;
	}

	public String getPbxNo() {
		return pbxNo;
	}

	public void setPbxNo(String pbxNo) {
		this.pbxNo = pbxNo;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public String getFullUserName() {
		return fullUserName;
	}

	public void setFullUserName(String fullUserName) {
		this.fullUserName = fullUserName;
	}

	public String getTelCo() {
		return telCo;
	}

	public void setTelCo(String telCo) {
		this.telCo = telCo;
	}
}
